package com.tengfei.adapterdemo.util;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtil {

    public static String doGet(String url) {
        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        StringBuilder stringBuilder = new StringBuilder();
        URL requesturl = null;
        try {
            // 创建URL
            requesturl = new URL(url);
            // 获取UrlConnection对象
            urlConnection = (HttpURLConnection) requesturl.openConnection();
            // 配置超时时间,5秒钟
            urlConnection.setConnectTimeout(8000);
            urlConnection.setReadTimeout(8000);
            // 请求数据格式
            urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            // 配置请求方式
            urlConnection.setRequestMethod("GET");
            // 发起请求
            urlConnection.connect();
            if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = urlConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String temp = "";
                while ((temp = reader.readLine()) != null) {
                    stringBuilder.append(temp);
                }
                reader.close();
                return stringBuilder.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }

    public static String doPost(String url, String jsonObject) {
        HttpURLConnection urlConnection = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        URL requesturl = null;
        String result = "";
        try {
            // 创建URL
            requesturl = new URL(url);
            // 获取UrlConnection对象
            urlConnection = (HttpURLConnection) requesturl.openConnection();
            // 配置超时时间,5秒钟
            urlConnection.setConnectTimeout(8000);
            urlConnection.setReadTimeout(8000);
            // 默认值为true，当向远程服务器读取数据时，设置为true,可写可不写
            urlConnection.setDoInput(true);
            // 默认为false,当向远程服务器发送数据时,设置为true
            urlConnection.setDoOutput(true);
            // 请求数据格式,(以json数据格式为主)
            urlConnection.setRequestProperty("Content-type", "application/json;charset=UTF-8");
            // 接收格式设置
            urlConnection.setRequestProperty("Accept-Language", "zh_CN,zh;q=0.9");
            urlConnection.setRequestProperty("Accept", "application/json,text/plan,*/*");
            // 配置请求方式
//            urlConnection.setUseCaches(false);
            urlConnection.setRequestMethod("POST");
            //map转string
//            String paramDate = paramMapToString(paramMap);
            // 将数据以字节流的方式传输出去
            byte[] bytes = jsonObject.getBytes("UTF-8");
            urlConnection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
            urlConnection.connect();
            // 获取一个输出流,将数据传输出去
            outputStream = urlConnection.getOutputStream();
            outputStream.write(bytes);
            if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = urlConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
                BufferedReader reader = new BufferedReader(inputStreamReader);
                result = reader.readLine();
                reader.close();
                inputStream.close();
                return result;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    // 获取stream流
    public static Bitmap doBitmap(String url) {
        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        StringBuilder stringBuilder = new StringBuilder();
        URL requesturl = null;
        Bitmap bitmap = null;
        try {
            // 创建URL
            requesturl = new URL(url);
            // 获取UrlConnection对象
            urlConnection = (HttpURLConnection) requesturl.openConnection();
            // 配置超时时间,5秒钟
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(false);
            // 配置请求方式
            urlConnection.setRequestMethod("GET");
            // 发起请求
            urlConnection.connect();
            if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = urlConnection.getInputStream();
                bitmap = BitmapFactory.decodeStream(inputStream);

                return bitmap;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }

}
